package com.lunwen.lunwen.api;

import com.lunwen.lunwen.bean.LunWenXinXi;
import com.lunwen.lunwen.bean.Result;
import com.lunwen.lunwen.bean.SysUser;
import com.lunwen.lunwen.bean.XueSheng;
import com.lunwen.lunwen.services.LunWenXinXiService;
import com.lunwen.lunwen.services.SysUserService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;
import springfox.documentation.spring.web.json.Json;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * (LunWenXinXi)表控制层
 *
 * @author makejava
 * @since 2020-07-21 11:36:48
 */
@RestController
@RequestMapping("api")
public class LunWenXinXiController {

    /**
     * 服务对象
     */
    @Autowired
    private LunWenXinXiService lunWenXinXiService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 新增
     * @param lunWenXinXi
     * @return
     */
    @RequestMapping("addlunwenxinxi")
    public Result addLunWenXinXi(@RequestBody LunWenXinXi lunWenXinXi){
        Result result = new Result();
        if (lunWenXinXi.getXueshengId() == null){
            result.setResult("201","学生用户ID不能为空！");
            return result;
        }
        if (lunWenXinXi.getDaoshiId() == null){
            result.setResult("201","导师用户ID不能为空！");
            return result;
        }
        if (StringUtils.isEmpty(lunWenXinXi.getLunwentimu())){
            result.setResult("201","论文题目不能为空！");
            return result;
        }
        SysUser sysUser = sysUserService.queryById(lunWenXinXi.getXueshengId());
        if (sysUser == null){
            result.setResult("201","不存在该学生！");
            return result;
        }else{
            if (sysUser.getType() != 1){
                result.setResult("201","该用户不是学生！");
                return result;
            }
        }
        SysUser sysUser1 = sysUserService.queryById(lunWenXinXi.getDaoshiId());
        if (sysUser1 == null){
            result.setResult("201","不存在该导师！");
            return result;
        }else{
            if (sysUser1.getType() != 0){
                result.setResult("201","该用户不是导师！");
                return result;
            }
        }
        LunWenXinXi lunWenXinXi1 = new LunWenXinXi();
        lunWenXinXi1.setXueshengId(lunWenXinXi.getXueshengId());
        List<LunWenXinXi> lunWenXinXis = lunWenXinXiService.queryAllByLimit(lunWenXinXi1, 0, 1);
        if (lunWenXinXis != null){
            if (lunWenXinXis.get(0) != null){
                result.setResult("200","已存在该学生的论文信息！");
                return result;
            }
        }
        String id = UUID.randomUUID().toString();
        lunWenXinXi.setId(id);
        LunWenXinXi insert = lunWenXinXiService.insert(lunWenXinXi);
        result.setResult("200","新增成功！",insert);
        return result;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("dellunwenxinxi")
    public Result delLunWenXinXi(@RequestParam("id") String id){
        Result result = new Result();
        if (StringUtils.isEmpty(id)){
            result.setResult("201","ID不能为空！");
            return result;
        }
        lunWenXinXiService.deleteById(id);
        result.setResult("200","删除成功！");
        return result;
    }

    /**
     * 修改
     * @param lunWenXinXi
     * @return
     */
    @RequestMapping("updatelunwenxinxi")
    public Result updateLunWenXinXi(@RequestBody LunWenXinXi lunWenXinXi){
        Result result = new Result();
        if (!StringUtils.isEmpty(lunWenXinXi.getXueshengId())){
            SysUser sysUser = sysUserService.queryById(lunWenXinXi.getXueshengId());
            if (sysUser == null){
                result.setResult("201","不存在该学生！");
                return result;
            }else{
                if (sysUser.getType() != 1){
                    result.setResult("201","该用户不是学生！");
                    return result;
                }
            }
        }
        if (!StringUtils.isEmpty(lunWenXinXi.getDaoshiId())){
            SysUser sysUser1 = sysUserService.queryById(lunWenXinXi.getDaoshiId());
            if (sysUser1 == null){
                result.setResult("201","不存在该导师！");
                return result;
            }else{
                if (sysUser1.getType() != 0){
                    result.setResult("201","该用户不是导师！");
                    return result;
                }
            }
        }
        LunWenXinXi update = lunWenXinXiService.update(lunWenXinXi);
        result.setResult("200","修改成功！",update);
        return result;
    }

    /**
     * 查询
     * @param yeMa
     * @param xueshengId
     * @param daoshiId
     * @param lunwentimu
     * @param timulaiyuan
     * @param timuleixing
     * @return
     */
    @RequestMapping("selectalllunwenxinxi")
    public Result selectAllLunWenXinXi(@RequestParam("yeMa") int yeMa,
                                       @RequestParam(required = false) Long xueshengId,
                                       @RequestParam(required = false) Long daoshiId,
                                       @RequestParam(required = false) String lunwentimu,
                                       @RequestParam(required = false) String timulaiyuan,
                                       @RequestParam(required = false) String timuleixing
                                    ){
        Result result = new Result();
        LunWenXinXi lunWenXinXi = new LunWenXinXi();
        lunWenXinXi.setXueshengId(xueshengId);
        lunWenXinXi.setDaoshiId(daoshiId);
        lunWenXinXi.setLunwentimu(lunwentimu);
        lunWenXinXi.setTimulaiyuan(timulaiyuan);
        lunWenXinXi.setTimuleixing(timuleixing);
        int limit = 10;
        int zongGeShu = lunWenXinXiService.queryCount(lunWenXinXi);
        int xiaBiao = (yeMa - 1) * limit;
        int zongYeMa = (zongGeShu - 1) / limit + 1;
        List<LunWenXinXi> lunWenXinXis = lunWenXinXiService.queryAllByLimit(lunWenXinXi, xiaBiao, limit);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("zongGeShu", zongGeShu);
        map.put("zongYeMa", zongYeMa);
        map.put("list", lunWenXinXis);
        result.setResult("200","查询成功！",map);
        return result;
    }


}